package com.yanyeori.framework.security.database;

import com.yanyeori.framework.core.util.BeanUtil;
import com.yanyeori.framework.datasource.annotation.DataSource;
import com.yanyeori.framework.security.bo.UserDetail;
import com.yanyeori.framework.security.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * User数据库Dao
 *
 * @author chenkuan 2020/11/20
 */
@Repository
@DataSource("security")
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据用户名查询用户信息
     *
     * @param username 用户名
     * @return UserDetail
     */
    public UserDetail getByUsername(String username) {
        String sql = "SELECT * FROM t_user WHERE username = ?";
        try {
            User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username);
            return convertToUserDetail(user);
        } catch (IncorrectResultSizeDataAccessException e) {
            return null;
        }
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id
     * @return UserDetail
     */
    public UserDetail getByUserId(String userId) {
        String sql = "SELECT * FROM t_user WHERE user_id = ?";
        try {
            User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), userId);
            return convertToUserDetail(user);
        } catch (IncorrectResultSizeDataAccessException e) {
            return null;
        }
    }

    /**
     * 插入用户
     *
     * @param user 用户信息
     */
    public int insert(User user) {
        String sql = "insert into t_user " +
                "( " +
                "user_id, " +
                "username, " +
                "nickname, " +
                "password, " +
                "pwd_update_time, " +
                "pwd_error_times, " +
                "status, " +
                "description, " +
                "create_user, " +
                "create_time, " +
                "update_user, " +
                "update_time " +
                ") " +
                "values " +
                "(?,?,?,?,?,?,?,?,?,?,?,?) ";
        return jdbcTemplate.update(sql,
                user.getUserId(),
                user.getUsername(),
                user.getNickname(),
                user.getPassword(),
                user.getPwdUpdateTime(),
                user.getPwdErrorTimes(),
                user.getStatus(),
                user.getDescription(),
                user.getCreateUser(),
                user.getCreateTime(),
                user.getUpdateUser(),
                user.getUpdateTime());
    }

    /**
     * 修改用户密码
     *
     * @param user        用户信息
     * @param newPassword 新密码
     */
    public int updatePwd(User user, String newPassword) {
        String sql = "update t_user " +
                "set " +
                "password = ?, " +
                "pwd_update_time = ?, " +
                "pwd_error_times = ?, " +
                "update_user = ?, " +
                "update_time = ? " +
                "where username = ? ";
        return jdbcTemplate.update(sql,
                newPassword,
                user.getPwdUpdateTime(),
                user.getPwdErrorTimes(),
                user.getUpdateUser(),
                user.getUpdateTime(),
                user.getUsername());
    }

    /**
     * 用户密码输入错误次数自增1
     *
     * @param username 用户名
     */
    public int pwdErrorTimeIncr(String username) {
        String sql = "update t_user " +
                "set " +
                "pwd_error_times = pwd_error_times + 1, " +
                "pwd_update_time = ? " +
                "where username = ? ";
        return jdbcTemplate.update(sql, System.currentTimeMillis(), username);
    }

    /**
     * 更新用户密码输入错误次数
     *
     * @param username      用户名
     * @param pwdErrorTimes 密码输入错误次数
     */
    public int updatePwdErrorTime(String username, int pwdErrorTimes) {
        String sql = "update t_user " +
                "set " +
                "pwd_error_times = ?, " +
                "pwd_update_time = ? " +
                "where username = ? ";
        return jdbcTemplate.update(sql, pwdErrorTimes, System.currentTimeMillis(), username);
    }

    private UserDetail convertToUserDetail(User user) {
        return BeanUtil.copyProperties(user, UserDetail.class);
    }
}
